package org.zht.dormpro.dorm.service.impl;

import com.xiaoleilu.hutool.util.CollectionUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.bean.MajorDTO;
import org.zht.dormpro.dorm.cacher.IMajorCacher;
import org.zht.dormpro.dorm.mapper.TbMajorMapper;
import org.zht.dormpro.dorm.model.TbMajor;
import org.zht.dormpro.dorm.service.IMajorService;

import java.util.ArrayList;
import java.util.List;

@Service
@Log4j
public class MajorServiceImpl implements IMajorService{

    @Autowired
    private TbMajorMapper tbMajorMapper;
    @Autowired
    private IMajorCacher majorCacher;

    @Override
    public List<MajorDTO> getMajorByCondition(PageBean pageBean) {
        List<MajorDTO> majorDTOS = tbMajorMapper.getAllMajorByCondition(pageBean);
        log.debug(majorDTOS);
        return majorDTOS;
    }

    @Override
    public boolean addMajor(MajorDTO majorDTO) {
        TbMajor tbMajor = new TbMajor();
        BeanUtils.copyProperties(majorDTO,tbMajor);
        log.info(tbMajor);
        int i = tbMajorMapper.insertSelective(tbMajor);
        return i > 0;
    }

    @Override
    public boolean deleteMajor(Integer id) {
        int i = tbMajorMapper.deleteByPrimaryKey(id);
        return i > 0;
    }

    @Override
    public boolean batchRemoveMajor(ArrayList<String> majIds) {
        if (CollectionUtil.isNotEmpty(majIds)) {
            for (String majId : majIds) {
                boolean b = deleteMajor(Integer.valueOf(majId));
                if (!b) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public TbMajor getMajorById(Integer id) {
        return tbMajorMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean editMajor(MajorDTO majorDTO) {
        TbMajor tbMajor = new TbMajor();
        BeanUtils.copyProperties(majorDTO,tbMajor);
        int i = tbMajorMapper.updateByPrimaryKeySelective(tbMajor);
        return i>0;
    }

    @Override
    public List<TbMajor> getAllMajor(int i) {
        return tbMajorMapper.getAllMajor(i);
    }

    @Override
    public List<TbMajor> getMajorByCollegeId(String colId) {
        //TODO 这个地方可以缓存---看IMajorCacher--除非需要改动
        //return tbMajorMapper.getMajorByCollegeId(colId);
        List<TbMajor> majorByCollegeId = majorCacher.getMajorByCollegeId(colId);
        return majorByCollegeId;

    }
}
